//
//  AppTabBarController.m
//  EnterpriseManagement
//
//  Created by Lwx on 16/10/28.
//  Copyright © 2016年 Lwx. All rights reserved.
//

#import "AppTabBarController.h"
#import "AppNavgiationController.h"
#import "MyTabBar.h"
#import "MineViewController.h"
#import "CityTravelViewController.h"
#import "DiscoverViewController.h"

#import "HomeViewController.h"
#import "CityListViewController.h"


@interface AppTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic,strong) MyTabBar *myTabBar;

@end

@implementation AppTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    HomeViewController *mainVC = [[HomeViewController alloc] init];
    [self addChildViewController:mainVC image:@"tab_home" selectedImage:@"tab_home_selected" title:@"主页"];
    
    CityTravelViewController *askVC = [[CityTravelViewController alloc]init];
    [self addChildViewController:askVC image:@"tab_xcyj" selectedImage:@"tab_xcyj_selected" title:@"小城游记"];
    
    UIViewController *contentVC = [[UIViewController alloc] init];
    [self addChildViewController:contentVC image:@"" selectedImage:@"" title:@""];
    
    UIViewController *chatListVC = [[DiscoverViewController alloc] init];
    [self addChildViewController:chatListVC image:@"tab_fxhd" selectedImage:@"tab_fxhd_selected" title:@"发现活动"];
    
    MineViewController *contactsVC1 = [[MineViewController alloc] init];
    [self addChildViewController:contactsVC1 image:@"tab_user" selectedImage:@"tab_user_selected" title:@"我的"];
    
    self.myTabBar = [[MyTabBar alloc] init];
    [self setValue:self.myTabBar forKey:@"tabBar"];
//    self.selectedIndex = 2;
}

/**
 *  添加子控制器
 *
 *  @param childViewController 子控制器
 *  @param image               tabBarItem正常状态图片
 *  @param selectedImage       tabBarItem选中状态图片
 *  @param title               标题
 */
- (void)addChildViewController:(UIViewController *)childViewController image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
{
    //标题
    childViewController.title = title;
    
    if (!childViewController.view.backgroundColor) {
        childViewController.view.backgroundColor = [UIColor whiteColor];
    }
    
    //tabBarItem图片
    childViewController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childViewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //tabBarItem字体的设置
    //正常状态
    NSMutableDictionary *normalText = [NSMutableDictionary dictionary];
    normalText[NSForegroundColorAttributeName] = [UIColor colorWithHex:0x808080];
    [childViewController.tabBarItem setTitleTextAttributes:normalText forState:UIControlStateNormal];
    
    //选中状态
    NSMutableDictionary *selectedText = [NSMutableDictionary dictionary];
    selectedText[NSForegroundColorAttributeName] = [UIColor themeColor];
    [childViewController.tabBarItem setTitleTextAttributes:selectedText forState:UIControlStateSelected];
    
    //导航控制器
    AppNavgiationController *navgationVC = [[AppNavgiationController alloc] initWithRootViewController:childViewController];
    
    [self addChildViewController:navgationVC];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    NSUInteger i = [tabBarController.viewControllers indexOfObject:viewController];
    if (i==2) {
        
        CityListViewController *cityListVC = [[CityListViewController alloc]init];
        cityListVC.arrayHotCity = [NSMutableArray arrayWithObjects:@"北京",@"上海",@"广州",@"厦门",@"深圳",@"天津",@"长沙",@"郑州", nil];
        cityListVC.arrayLocatingCity = [NSMutableArray arrayWithObjects:@"沈阳", nil];
        cityListVC.view.backgroundColor = [UIColor whiteColor];
        [[UIViewController getCurrentVC].navigationController pushViewController:cityListVC animated:YES];

        return NO;
        
    }else{
        self.myTabBar.plusBtn.selected = NO;
        self.myTabBar.titleLabel.textColor = [UIColor colorWithHex:0x808080];
    }
    return YES;
}


@end
